<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="课程名称" prop="courseId">
        <el-select v-model="queryParams.courseId" placeholder="请选择课程" clearable @change="changeQueryCourse">
          <el-option v-for="dict in data.courseList" :key="dict.courseId" :label="dict.courseName"
            :value="dict.courseId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="classId">
        <el-select v-model="queryParams.classId" placeholder="请选择课程后再选择班级" clearable>
          <el-option v-for="dict in data.classList" :key="dict.classId" :label="dict.className" :value="dict.classId" />
        </el-select>
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="姓名" align="center" prop="name" width="100" />
      <el-table-column label="报名状态" align="center" width="100">
        <template #default="scope">
          <span>{{ scope.row.applicationStatus === '0' ? '已报名' : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名时间" align="center" prop="createTime" />
      <el-table-column label="开课时间" align="center" prop="openTime" />
      <el-table-column label="联系人" align="center" prop="teacherName" />
      <el-table-column label="总学时" align="center" prop="allTime" />
      <el-table-column label="归属班级" align="center" prop="className" />
      <el-table-column label="归属班主任" align="center" prop="headName" />

      <el-table-column label="详情查看" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleSeekInfo(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学期对话框 -->
    <el-dialog title="报名详情" v-model="open" width="1023px" append-to-body>
      <AllInfo :info="params" v-if="open"></AllInfo>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="open = false">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  
<script setup>
import AllInfo from './components/allInfo.vue'
import { queryApplicationList, queryCourseList } from "@/api/home.js";
import { queryClassByCourseId } from "@/api/register/registerList.js";

const { proxy } = getCurrentInstance();

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const total = ref(0);
const params = ref({})
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    courseId: null,
    classId: null,
    name: null,
  },
  courseList: [],
  classList: []
});
const { queryParams } = toRefs(data);
const handleSeekInfo = (item) => {
  params.value = item
  open.value = true
}
/** 查询学期列表 */
function getList() {
  loading.value = true;
  queryApplicationList(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询课程 */
function getCourse() {
  queryCourseList().then((response) => {
    console.log("🚀 ~ queryCourseList ~ response:", response)
    if (response.code === 200 && response.rows.length) {
      data.courseList = response.rows;
      console.log("🚀 ~ queryCourseList ~ courseList.value:", data.courseList)
    }
  });
}

/** 根据课程id查询班级列表*/
function queryClassList(courseId) {
  queryClassByCourseId({ courseId }).then((response) => {
    if (response.data && response.data.length) {
      data.classList = response.data;
    }
  });
}
function changeQueryCourse(courseId) {
  data.classList = []
  data.queryParams.classId = null
  if (courseId) {
    queryClassList(courseId);
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
getList();
getCourse()
</script>
  